package com.muqi.app.qlearn.student.ui;

import com.muqi.app.project.contants.MContants;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class InvitationFriendActivity extends BaseFragmentActivity implements OnClickListener {
	private final UMSocialService mController = UMServiceFactory.getUMSocialService(MContants.UM_SHARE);
	private TextView tv_record;
	private ImageView iv_weicir;
	private ImageView iv_weifri;
	private ImageView iv_qqfri;
	private ImageView iv_qqkong;
	private String title="分享好友得积分兑换礼品，让孩子快乐成长";
	private String url=MContants.SHARE_URL;
    private String shareContent="悦成长,让孩子快乐成长";
    
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_invitation_friend);
		tv_record = (TextView) findViewById(R.id.tv_record);
		iv_qqfri = (ImageView) findViewById(R.id.iv_share_qqfri);
		iv_qqkong = (ImageView) findViewById(R.id.iv_share_qqkong);
		iv_weicir = (ImageView) findViewById(R.id.iv_share_weicir);
		iv_weifri = (ImageView) findViewById(R.id.iv_share_weifri);

		iv_qqfri.setOnClickListener(this);
		iv_qqkong.setOnClickListener(this);
		iv_weicir.setOnClickListener(this);
		iv_weifri.setOnClickListener(this);
		configPlatforms();
	}
	
	private void configPlatforms() {
        // 添加新浪SSO授权
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
        // 添加腾讯微博SSO授权
       // mController.getConfig().setSsoHandler(new TencentWBSsoHandler());
        // 添加QQ、QZone平台
        addQQQZonePlatform();
        // 添加微信、微信朋友圈平台
        addWXPlatform();
    }
	
	/**
     * @功能描述 : 添加QQ平台支持 QQ分享的内容， 包含四种类型， 即单纯的文字、图片、音乐、视频. 参数说明 : title, summary,
     *       image url中必须至少设置一个, targetUrl必须设置,网页地址必须以"http://"开头 . title :
     *       要分享标题 summary : 要分享的文字概述 image url : 图片地址 [以上三个参数至少填写一个] targetUrl
     *       : 用户点击该分享时跳转到的目标地址 [必填] ( 若不填写则默认设置为友盟主页 )
     * @return
     */
    private void addQQQZonePlatform() {
        // 添加QQ支持, 并且设置QQ分享内容的target url
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(InvitationFriendActivity.this, MContants.QQ_App_Id, MContants.QQ_App_Key);
        qqSsoHandler.setTitle(title);
        qqSsoHandler.setTargetUrl(url);
        qqSsoHandler.addToSocialSDK();
        // 添加QZone平台
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(InvitationFriendActivity.this, MContants.QQ_App_Id, MContants.QQ_App_Key);
        qZoneSsoHandler.addToSocialSDK();
    }
    
    /**
     * @功能描述 : 添加微信平台分享
     * @return
     */
    private void addWXPlatform() {
        // 注意：在微信授权的时候，必须传递appSecret
        // wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
        // 添加微信平台
        UMWXHandler wxHandler = new UMWXHandler(InvitationFriendActivity.this, MContants.WX_APP_ID, MContants.WX_APP_KEY);
        wxHandler.setTitle(title);
        wxHandler.setTargetUrl(url);
        wxHandler.addToSocialSDK();
        // 支持微信朋友圈
        UMWXHandler wxCircleHandler = new UMWXHandler(InvitationFriendActivity.this, MContants.WX_APP_ID, MContants.WX_APP_KEY);
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.setTitle(title);
        wxCircleHandler.setTargetUrl(url);
        wxCircleHandler.addToSocialSDK();
      
    }
    

	@Override
	public void onClick(View arg0) {
		UMImage image = new UMImage(InvitationFriendActivity.this,
				R.drawable.um_share_logo);
		switch (arg0.getId()) {
		case R.id.tv_record:

			break;
		case R.id.iv_share_qqfri:
			// QQ content
			QQShareContent qqShareContent = new QQShareContent();
			qqShareContent.setShareContent(shareContent);
			qqShareContent.setTitle(title);  //不加的话，title默认为“分享到QQ”
			qqShareContent.setShareImage(image);
			qqShareContent.setTargetUrl(url);
			mController.setShareMedia(qqShareContent);
			mController.postShare(mContext,SHARE_MEDIA.QQ,null);
			break;
		case R.id.iv_share_qqkong:
			// QZone content   
			// FIXME:一旦调用mController.setShareMedia(qzone)方法，则mController之前的设置在qzone中都不会生效,必须在QZoneShareContent重新设置。QQ也一样。
			QZoneShareContent qzone = new QZoneShareContent();
			qzone.setShareContent(shareContent);
			qzone.setTargetUrl(url);
			qzone.setTitle(title);
			qzone.setShareImage(image);
			//qzone.setShareImage(urlImage); //尽管mController设置图片，若此处不设置图片，则分享时没有图片
			mController.setShareMedia(qzone);
			mController.postShare(mContext,SHARE_MEDIA.QZONE,null);
			break;
		case R.id.iv_share_weicir:
		

			//设置微信朋友圈分享内容
			CircleShareContent circleMedia = new CircleShareContent();
			circleMedia.setShareContent(shareContent);
			//设置朋友圈title
			circleMedia.setTitle(title);
			circleMedia.setShareImage(image);
			circleMedia.setTargetUrl(url);
			mController.setShareMedia(circleMedia);
			mController.postShare(mContext,SHARE_MEDIA.WEIXIN_CIRCLE,null);
			break;
		case R.id.iv_share_weifri:
			//设置微信好友分享内容
			WeiXinShareContent weixinContent = new WeiXinShareContent();
			//设置分享文字
			weixinContent.setShareContent(shareContent);
			//设置title
			weixinContent.setTitle(title);
			//设置分享内容跳转URL
			weixinContent.setTargetUrl(url);
			//设置分享图片
			weixinContent.setShareImage(image);
			mController.setShareMedia(weixinContent);
			mController.postShare(mContext,SHARE_MEDIA.WEIXIN,null);
			break;
		default:
			break;
		}

	}
	public void onBack(View v) {
		this.finish();
	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onData() {
		// TODO Auto-generated method stub
		
	}
	


}
